﻿Imports System.Data.OleDb
Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("booksConn").ConnectionString)
            Dim SqlString As String = "SELECT * FROM UserProfile where TravellerName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()

            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows() Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then Tb_firstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then Tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Gender")) Then Tb_gender.Text = dr("Gender")
                If Not IsDBNull(dr("EmailId")) Then Tb_email.Text = dr("EmailId")
                If Not IsDBNull(dr("DateOfBirth")) Then Tb_dob.Text = dr("DateOfBirth")
            End If
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub Btn_profile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_profile.Click
        Dim myGUID = Guid.NewGuid()
        Dim SqlString As String = ""
        Dim newFileName As String = ""
        If Fu_picture.HasFile Then
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("pictures") & "/" & newFileName
            Fu_picture.SaveAs(fileLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("booksConn").ConnectionString)
        If Fu_picture.HasFile Then
            SqlString = "Update UserProfile SET FirstName=@f1,LastName=@f2,Gender=@f3,UserPicture=@f4,DateOfBirth=@f5,EmailId=@f6 where TravellerName=@f7"
        Else
            SqlString = "Update UserProfile SET FirstName=@f1,LastName=@f2,Gender=@f3,DateOfBirth=@f5,EmailId=@f6 where TravellerName=@f7"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", Tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", Tb_gender.Text)
        If Fu_picture.HasFile Then
            cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        End If
        Convert.ToDateTime(Tb_dob.Text)
        cmd.Parameters.AddWithValue("@f5", Convert.ToDateTime(Tb_dob.Text))
        cmd.Parameters.AddWithValue("@f6", Tb_email.Text)
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("profile.aspx")

    End Sub
End Class